'use strict'
let request = require('superagent');
let cheerio = require('cheerio');
const async = require('async');
const url = require('url');
const config = require('./config');
const _ = require('lodash');

const parseVideo = require('./parseVideo');

let get_courses = class_urls => {
    async.mapLimit(class_urls, 2, (cl, callback) => {
        console.log('enter :' + cl);
        request.get(cl)
            .set(config.headers)
            .end((err, res) => {
                // console.log('handler ' + cl);
                if (err) {
                    callback(null, Object.assing({
                        title: '',
                        href: cl
                    }, {
                        success: 'no',
                        err: err,
                        data: null
                    }));
                } else {
                    // console.log(res.text);
                    let course_prefix = '/course/'
                    try {
                        course_prefix += cl.match(/\d+/)[0]
                    } catch (error) {
                        console.error('this course url is not fit this regex', error);
                    }
                    let $ = cheerio.load(res.text);
                    let item_course_url = [];
                    let title = $('title').text().split('-')[0];
                    $(`a[href|=${course_prefix}]`).each(function() {
                        console.log(this);
                        item_course_url.push(url.resolve(config.base_url, $(this).attr('href')))
                    })
                    callback(null, Object.assign({ title: title, href: cl }, { success: 'ok', err: null, data: item_course_url }));
                }
            })
    }, (err, resulst) => {
        if (err) {
            console.log(err);
        } else {
            let frevs = resulst
                .filter(item => item.success == 'ok')
                .map(item => item.data)
            parseVideo(_.flatten(frevs));
        }
    });

}
module.exports = {
    get_courses: get_courses
}